<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserAction
 *
 * @author Administrator
 */
import('@.Filter.BaseAction');
import('@.Filter.UserSecurityAction');
import('@.Service.UserService');
import('@.Service.CommentService');
import('@.Service.RecommendationService');
import('@.Util.ConstantUtil');
import('ORG.Util.Page');
vendor('sinaweibo.saetv2#ex#class');

class UserAction extends UserSecurityAction {

    function register() {
        $userService = new UserService();
        $user = $userService->create();

//        dump($user);
        if (empty($user['email']) && empty($user['password']) && empty($_REQUEST['check_code'])) {
            $this->assign('content', 'register');
            $this->display('Subject:layout_subject');

            return;
        }

        $validationCode = $_REQUEST['check_code'];
        if (empty($validationCode)) {
            $this->assign('error', '验证码为空');
            $this->assign('content', 'register');
            $this->display('Subject:layout_subject');

            return;
        }
        if (md5($validationCode) != Session::get('verify')) {

            $this->assign('error', '验证码错误');
            $this->assign('content', 'register');
            $this->display('Subject:layout_subject');

            return;
        }
        $user['password'] = md5($user['password']);
        $userService->insert($user);
        SESSION::set(ConstantUtil::$WEB_USER, $user);

        $this->redirect('Index/index');
    }

    function isHas() {
        $userService = new UserService();
        $email = $_POST['email'];

        if (is_null($userService->findUserByEmail($email))) {
            echo '1';
        } else {
            echo '0';
        }
    }

    function login() {
        $userService = new UserService();
        $user = $userService->create();
//dump($user);return;
        $loginForwardFlag = 'loginForwardFlag';
        if (empty($user['email']) && empty($user['password'])) {
            if (!Cookie::is_set($loginForwardFlag)) {
                //记录登陆前的页面链接
                $httpReferer = $_SERVER ["HTTP_REFERER"];
                Cookie::set($loginForwardFlag, $httpReferer);
            }

            $this->assign('content', 'login');
            $this->display('Subject:layout_subject');
            return;
        }
        $user = $userService->validUser($user);
//        dump(D('user')->getLastSql());
//        return;

        if (empty($user)) {
            $this->assign('content', 'login');
            $this->display('Subject:layout_subject');
            return;
        }
        //更新登陆时间
        $user['last_datetime'] = $user['this_datetime'];
        $user['this_datetime'] = ConstantUtil::date();

        $userService->update($user);

        $user['password'] = '';
        SESSION::set(ConstantUtil::$WEB_USER, $user);
        $httpReferer = Cookie::get($loginForwardFlag);
        Cookie::delete($loginForwardFlag);




        //页面跳转
        header("location:" . $httpReferer);
    }

    function isLogin() {
        $this->ajaxReturn(Session::is_set(ConstantUtil::$WEB_USER));
    }

    /**
     * ajax登陆
     * @return type 
     */
    function ajaxLogin() {
        $userService = new UserService();
        $user['email'] = $_REQUEST['email'];
        $user['password'] = $_REQUEST['password'];
//        dump($user);
        $user = $userService->validUser($user);
//dump($user);
        if (empty($user)) {
            $this->ajaxReturn(false);
            return;
        }
        $user['password'] = '';
        SESSION::set(ConstantUtil::$WEB_USER, $user);

        $this->ajaxReturn(true);
    }

    function ajaxComment() {
        $commentService = new CommentService();
        $comment['user_id'] = $_REQUEST['user_id'];
        $comment['app_id'] = $_REQUEST['app_id'];
        $comment['comment'] = $_REQUEST['comment'];
        $comment['datetime'] = date('Y-m-d H:m:s');

        $id = $_POST['id'];
        if (!empty($id)) {
            $commentResult = $commentService->findCommentById($id);
        } else {
            $commentResult = $commentService->findCommentByUIdAId($comment['user_id'], $comment['app_id']);
        }


        if (empty($commentResult)) {
            $latId = $commentService->insert($comment);
            if ($latId > 0) {
                $this->ajaxReturn(1);

                return;
            }
        } else {
            $commentResult['comment'] = $comment['comment'];
            $commentResult['datetime'] = ConstantUtil::date();
            $latId = $commentService->update($commentResult);

            if ($latId > 0) {
                $this->ajaxReturn(1);

                return;
            }
        }

        $this->ajaxReturn(0);
    }

    /**
     * 当前用户是否发过评论
     * @return type 
     */
    function ajaxCommentText() {
        $commentService = new CommentService();
        $comment['user_id'] = $_REQUEST['user_id'];
        $comment['app_id'] = $_REQUEST['app_id'];

        $id = $_POST['id'];
        if (!empty($id)) {
            $comment = $commentService->findCommentById($id);
        } else {
            $comment = $commentService->findCommentByUIdAId($comment['user_id'], $comment['app_id']);
        }
        if (empty($comment)) {
            $this->ajaxReturn(0);
            return;
        }

        $this->ajaxReturn($comment);
    }

    /**
     * 评分
     */
    function ajaxScore() {
        $commentService = new CommentService();
        $comment['user_id'] = $_REQUEST['user_id'];
        $comment['app_id'] = $_REQUEST['app_id'];
        $comment['score'] = $_REQUEST['rate'];

        $commentResult = $commentService->findCommentByUIdAId($comment['user_id'], $comment['app_id']);

        if (empty($commentResult)) {
            $latId = $commentService->insert($comment);
            if ($latId > 0) {
                $this->ajaxReturn(1);

                return;
            }
        } else {
            $commentResult['score'] = $comment['score'];
            $latId = $commentService->update($commentResult);

            if ($latId > 0) {
                $this->ajaxReturn(1);

                return;
            }
        }

        $this->ajaxReturn(0);
    }

    function logout() {
//        $o = new SaeTOAuthV2(ConstantUtil::$WEIBO_SINA_APP_KEY, ConstantUtil::$WEIBO_SINA_APP_SECRET);
        $user = Session::get(ConstantUtil::$WEB_USER);
//        dump($user);
        $url = "http://api.t.sina.com.cn/account/end_session.json?access_token=" . $user["token"]["access_token"];
        //dump($url);
        //dump(file_get_contents($url));
        //return;
        SESSION::destroy();
        header("location:" . $_SERVER ["HTTP_REFERER"]);
    }

    function profile() {
        $userService = new UserService();
        $user = $userService->create();
        if (empty($user['id'])) {
            $this->assign('content', 'profile');
            $this->display('Subject:layout_subject');

            return;
        }
        $userData = $userService->findUserById($user['id']);

        $userData = ConstantUtil::copyProperties($user, $userData, array('nick_name', 'blog', 'self_introduction'));
        //dump($userData);
        //return;
        $userService->update($userData);
        Session::set(ConstantUtil::$WEB_USER, $userData);

        $this->assign('web_user', Session::get(ConstantUtil::$WEB_USER));

        $this->assign('content', 'profile');
        $this->display('Subject:layout_subject');
    }

    function avatar() {
        $userService = new UserService();
        $id = $_POST['id'];

        if (empty($id)) {
            $this->assign('content', 'avatar');
            $this->display('Subject:layout_subject');

            return;
        }

        $user = $userService->findUserById($id);
        $user['avatar'] = $_POST['avatar'];
        $userService->update($user);

        Session::set(ConstantUtil::$WEB_USER, $user);

        $this->ajaxReturn(1);
    }

    function account() {
        //修改
        if (!empty($_REQUEST['modify'])) {
            $this->assign('modify', 'modify');
        }

        $old_password = $_POST['old_password'];
        $new_password1 = $_POST['new_password1'];
        if (!empty($old_password) && !empty($new_password1)) {
            $userService = new UserService();
            $user = Session::get(ConstantUtil::$WEB_USER);
            $user['password'] = md5($old_password);

            $user = $userService->findUserByEmailPassword($user);

            if (empty($user)) {
                //原密码错误
                $this->assign('content', 'account');
                $this->display('Subject:layout_subject');
                return;
            }

            $user['password'] = md5($new_password1);
            $userService->update($user);
        }

        $this->assign('user', Session::get(ConstantUtil::$WEB_USER));
        $this->assign('content', 'account');
        $this->display('Subject:layout_subject');
    }

    function binding() {


        $this->assign('content', 'binding');
        $this->display('Subject:layout_subject');
    }

    function mycomment() {
        $commentService = new CommentService();
        $user = Session::get(ConstantUtil::$WEB_USER);
        $count = $commentService->countCommentByUId($user['id']);

        $page = new Page($count, 8);

        $show = $page->show();
        $field = 'a.id a_id, a.name, a.tag, a.app_icon, a.app_icon_type, comment.*';
        $list = $commentService->listCommentByUId($user['id'], $field, $page->firstRow . ',' . $page->listRows);
//        dump(D('comment')->getLastSql());

        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->assign('show', $show);
        $this->assign('content', 'mycomment');
        $this->display('Subject:layout_subject');
    }

    //我的推荐
    function myrecommendation() {
        $rService = new RecommendationService();

        $user = Session::get(ConstantUtil::$WEB_USER);
        $count = $rService->countByUId($user['id']);
        $page = new Page($count, 8);

        $show = $page->show();
        $list = $rService->listByUId($user['id'], $page->firstRow . ',' . $page->listRows);
//        dump(D('comment')->getLastSql());

        $this->assign('count', $count);
        $this->assign('list', $list);
        $this->assign('show', $show);

        $this->assign('content', 'myrecommendation');
        $this->display('Subject:layout_subject');
    }

    //添加我的推荐
    function addRecommend() {
        $rService = new RecommendationService();
        //更新推荐
        $id = $_REQUEST['id'];

        $r = $rService->create();
        if (!empty($id) && empty($r)) {
            $r = $rService->findRById($id);

            $this->assign('r', $r);
            $this->assign('content', 'addRecommend');
            $this->display('Subject:layout_subject');
            return;
        }

        if (empty($r)) {

            $this->assign('content', 'addRecommend');
            $this->display('Subject:layout_subject');
            return;
        }

        if (!empty($r['id'])) {
            $rService->update($r);
        } else {
            $user = Session::get(ConstantUtil::$WEB_USER);

            $r['user_id'] = $user['id'];
            $r['datetime'] = date('Y-m-d H:m:s');
            $rService->insert($r);
        }

        $this->redirect('myrecommendation');
    }

    function deleteComment() {
        $id = $_POST['id'];

        if (!empty($id)) {
            $commentService = new CommentService();

            $commentService->delete($id);
        }

        $this->redirect('mycomment');
    }

    //删除我的推荐
    function deleteR() {
        $id = $_POST['id'];

        if (!empty($id)) {
            $rService = new RecommendationService();

            $rService->delete($id);
        }

        $this->redirect('myrecommendation');
    }

}
?>

